package com.anyikang.volunteer.sos.volunteer.app.mine;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.MyApplication;
import com.anyikang.volunteer.sos.volunteer.app.MyConstant;
import com.anyikang.volunteer.sos.volunteer.app.bean.User;
import com.anyikang.volunteer.sos.volunteer.app.userInfo.LoginActivity;
import com.anyikang.volunteer.sos.volunteer.utils.MyErrorDeal;
import com.anyikang.volunteer.sos.volunteer.utils.PageUtils;
import com.anyikang.volunteer.sos.volunteer.utils.okhttputils_callback.StringDialogCallback;
import com.lzy.okhttputils.OkHttpUtils;
import org.json.JSONObject;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;



public class SettingsActivity extends AppCompatActivity  implements View.OnClickListener{
    Button btLogout;
    RelativeLayout relayoutModifyPwd;
    String TAG = "SettingsActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        initView();
    }

    /**
     * 初始化视图控件
     */
    private void initView() {

        //set header
        TextView tvTitle = (TextView) this.findViewById(R.id.tv_title);
        tvTitle.setText("设置");
        ImageButton imgBtLeft = (ImageButton) this.findViewById(R.id.imgBtLeft);
        imgBtLeft.setOnClickListener(this);
        //Button
        btLogout = (Button) this.findViewById(R.id.btLogout);
        btLogout.setOnClickListener(this);
        relayoutModifyPwd = (RelativeLayout) this.findViewById(R.id.relayoutModifyPwd);
        relayoutModifyPwd.setOnClickListener(this);
    }


    /**
     * 用户退出后清除本地缓存的userInfo,并更换视图
     */

    public void dealLoginOut()
    {
        ((MyApplication)this.getApplication()).exit();
        User user = new User();
        user.userLogOut(this,0);  //退出登录时，不清用户名和密码
        finish();
        PageUtils.gotoLogin(this);
    }
    /**
     * 退出登录
     */
    public void exitLogin(String token)
    {
        Log.i(TAG, MyConstant.API_LOGIN_OUT+"|"+"token="+token);
        OkHttpUtils.post(MyConstant.API_LOGIN_OUT)//
                .tag(MyConstant.API_LOGIN_OUT)//
                .params("tokenId", token)//
                .execute(new StringDialogCallback(SettingsActivity.this) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {


                        int retCode = response.code();
                        if(retCode == 200)
                        {           Log.i(TAG,"onSuccess 200 return："+s);
                            try
                            {
                                JSONObject loginResult = new JSONObject(s);
                                if(loginResult.getString("status").equals("1"))//注册成功后跳转到首页
                                {
                                    //处理退出成功
                                    dealLoginOut();
                                }
                                else {

                                    String msg = loginResult.getString("msg");
                                    Log.i(TAG,msg);
                                   // Toast.makeText(SettingsActivity.this,"退出失败",Toast.LENGTH_LONG).show();
                                }
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                            finally {
                                dealLoginOut();
                            }
                        }
                        else
                        {
                            Log.i(TAG,"onSuccess "+retCode+"return: "+s);

                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {

                        dealLoginOut();//发生错误也要退出，否则永远退出不了
                        MyErrorDeal.dealHttpError(response,SettingsActivity.this);
                    }
                });


        return;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btLogout:
                User user = new User(SettingsActivity.this);
                exitLogin(user.getUserToken());
                break;
            case R.id.imgBtLeft:
                SettingsActivity.this.finish();
                break;
            case R.id.relayoutModifyPwd:  //修改密码
              Intent i = new Intent(SettingsActivity.this,ModifyPasswordActivity.class);
                startActivity(i);
                break;
        }

    }
}
